#
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License;
# you may not use this file except in compliance with the Elastic License.
#
include $(CPP_SRC_HOME)/mk/defines.mk

TARGET=$(OBJS_DIR)/libMlVer$(STATIC_LIB_EXT)

USE_BOOST=1

# make clean should remove the generated source file in addition to object files
CLEAN_CMDS=$(RM) CBuildInfo.cc

# Prepare values to substitute into the template
BUILD_YEAR=$(shell date '+%Y')
USER_NAME_CMD=id | awk -F')' '{ print $$1 }' | awk -F'(' '{ print $$2 }'
USER_NAME=$(shell $(USER_NAME_CMD))
PRODUCT_VERSION:=$(shell cat $(CPP_SRC_HOME)/gradle.properties | grep '^elasticsearchVersion' | awk -F= '{ print $$2 }' | xargs echo)
ifneq ($(VERSION_QUALIFIER),)
PRODUCT_VERSION:=$(PRODUCT_VERSION)-$(VERSION_QUALIFIER)
endif
ifeq ($(SNAPSHOT),yes)
PRODUCT_VERSION:=$(PRODUCT_VERSION)-SNAPSHOT
endif
ML_BUILD_NUM=$(shell git rev-parse --short=14 HEAD)
ifeq ($(OS),Windows)
# On Windows ignore file mode differences, as file modes don't really exist on Windows
UNCOMMITTED_CHANGES=$(shell git -c core.fileMode=false update-index -q --refresh > /dev/null ; git -c core.fileMode=false diff-index --quiet HEAD -- && echo no || echo yes)
else
UNCOMMITTED_CHANGES=$(shell git update-index -q --refresh > /dev/null ; git diff-index --quiet HEAD -- && echo no || echo yes)
endif

# Decide which template to use based on whether there are uncommitted changes
ifeq ($(UNCOMMITTED_CHANGES),no)
TEMPLATE=tagged_template
else
TEMPLATE=dev_template
endif

all: build

SRCS= \
CBuildInfo.cc \

include $(CPP_SRC_HOME)/mk/staticlib.mk

# Declaring this target PHONY means CBuildInfo.cc always gets rebuilt.  This is
# done because CBuildInfo.cc depends on the date and environment variables as
# well as the template.
.PHONY: force

CBuildInfo.cc: force
	@sed "s/@build.year@/$(BUILD_YEAR)/" < CBuildInfo.cc.$(TEMPLATE) \
		| sed "s/@warning.comment@/DO NOT EDIT THIS FILE - instead edit BOTH templates/" \
		| sed "s/@user.name@/$(USER_NAME)/" \
		| sed "s/@version.number@/$(PRODUCT_VERSION)/" \
		| sed "s/@build.number@/$(ML_BUILD_NUM)/" > CBuildInfo.cc.tmp
	@cmp -s CBuildInfo.cc CBuildInfo.cc.tmp || $(MV) CBuildInfo.cc.tmp CBuildInfo.cc && $(RM) CBuildInfo.cc.tmp

